<?php  

require_once(APPPATH.'controllers/epicentr.php');

class Reporting extends Epicentr {

	public function Reporting() {
		parent::Epicentr();
		
		$this->view_data['maintab'] = 'reporting';		
	}

	public function index() {
		$this->studiostatus();
	}
	
	public function studiostatus() {
		$this->load->model('jobsmodel');
		
		$this->view_data['page_js'][] = 'reports';
		
		$fulljoblist = $this->jobsmodel->getFullJobStatus( );
		$this->view_data['fulljoblist'] = $fulljoblist;
		
		$this->layout->buildPage('reporting/report-index', $this->view_data);
    }
    
	public function projection() {
		$this->load->model('jobsmodel');
		
		$jobvalues = $this->jobsmodel->getJobValues();
		
		if ($jobvalues) {		
			$monthlyjobvalues = $this->jobsmodel->getJobValuesByMonth();
		
			$firstmonth = reset($monthlyjobvalues)->tyear .'-'. reset($monthlyjobvalues)->tmonth.'-01';
			$lastmonth = end($monthlyjobvalues)->tyear .'-'. end($monthlyjobvalues)->tmonth.'-01';
		
			$dates = $this->month_range($firstmonth, $lastmonth);
		
			$proposed = array();
			$live = array();
		
			$minvalue = 0;
			$maxvalue = 0;
		
			foreach ($dates as $key => $date) {
				foreach($monthlyjobvalues as $value) {
					$thisdate = date('Y-m',strtotime($value->tyear.'-'.$value->tmonth.'-01'));
					if ( $thisdate == $date) {				
						if ($value->job_status == 'proposed') $proposed[] = $value->totalvalue;
						elseif ($value->job_status == 'live') $live[] = $value->totalvalue;
					
						if ($value->totalvalue > $maxvalue) $maxvalue = $value->totalvalue;
						if ($value->totalvalue < $minvalue) $minvalue = $value->totalvalue;
					}
				}
			
				if ( !isset($proposed[$key]) ) $proposed[$key] = -1;
				if ( !isset($live[$key]) ) $live[$key] = -1;
			}
		
			$range = array();
		
			for ($x = 0; $x < intval($maxvalue); $x += 1000 ) $range[] = $x;
		
			$chart = "http://chart.apis.google.com/chart?cht=bvs&chs=800x300&chd=t:".implode(',',$proposed)."|".implode(',',$live)."&chco=4d89f9,c6d9fd&chbh=60&chds=".$minvalue.",".$maxvalue."&chxt=x,y&chxl=0:|".implode('|', $dates)."|1:|".implode('|', $range)."|".$maxvalue."&chdl=Proposed|Live";
		
			$this->view_data['charturl'] = $chart;
		}
		$this->view_data['jobvalues'] = $jobvalues;
		
		$this->layout->buildPage('reporting/projection', $this->view_data);
	}

    public function invoicing($type = false) {
    	$this->load->model('invoicemodel');
    	
		$paidinvoices = $this->invoicemodel->getInvoices(false, 1, 1);
		$unpaidinvoices = $this->invoicemodel->getInvoices(false, 1, 0);
		$unprocessedinvoices = $this->invoicemodel->getInvoices(false, 0, 0);
		
    	switch ($type) {
    		case "paid":
    			$invoices = $paidinvoices;
    			$this->view_data['type'] = "paid";
    		break;
    		case "unpaid":
    			$invoices = $unpaidinvoices;
    			$this->view_data['type'] = "unpaid";
    		break;
    		default:
    			$invoices = $unprocessedinvoices;
    			$this->view_data['type'] = "default";
    		break;
    	}

		$maxvalue = max(count($unprocessedinvoices), count($unpaidinvoices), count($paidinvoices));
    	
		$chart = "http://chart.apis.google.com/chart?cht=bvs&chs=300x200&chbh=60&chd=t:".count($unprocessedinvoices).",".count($unpaidinvoices).",".count($paidinvoices)."&chco=c6d9fd&chds=0,".$maxvalue."&chxt=x,y&chxl=0:|Unprocessed|Unpaid|Paid|1:|0|".$maxvalue."";
		
		$this->view_data['charturl'] = $chart;

    	$this->view_data['invoices'] = $invoices;
    	
    	$this->layout->buildPage('reporting/invoices' , $this->view_data);
    }
	
	function month_range($sd,$ed)
	{
	$tmp = array();
	$sdu = strtotime($sd);
	$edu = strtotime($ed);
	while ($sdu <= $edu) {
	$tmp[] = date('Y-m',$sdu);
	$sdu = strtotime('+1 month',$sdu);
	}
	return ($tmp);
	}
 
}
?>